package com.beta.user.auth.center.service.provider;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.beta.user.auth.center.api.model.WxOauthUser;
import com.beta.user.auth.center.api.request.AddWxOauthUserReq;
import com.beta.user.auth.center.api.request.QueryWxOauthUserReq;
import com.beta.user.auth.center.api.request.UpdateWxOauthUserReq;
import com.beta.user.auth.center.api.response.AddWxOauthUserResp;
import com.beta.user.auth.center.api.response.CommonResultEnum;
import com.beta.user.auth.center.api.response.QueryWxOauthUserResp;
import com.beta.user.auth.center.api.response.UpdateWxOauthUserResp;
import com.beta.user.auth.center.api.service.IRPCWxOauthUserService;
import com.beta.user.auth.center.service.mapper.model.MpAuthAccount;
import com.beta.user.auth.center.service.service.IWxOauthUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author gwqin
 * @description
 * @date 2023-07-11 16:22
 */
@Slf4j
@Service
@DubboService
public class RPCWxOauthUserServiceImpl implements IRPCWxOauthUserService {

    @Autowired
    private IWxOauthUserInfoService userInfoService;

    @Override
    public AddWxOauthUserResp addWxOauthUser(AddWxOauthUserReq req) {
        AddWxOauthUserResp resp = new AddWxOauthUserResp();
        WxOauthUser wxOauthUser = req.getWxOauthUser();
        if (wxOauthUser == null
                || StringUtils.isAllBlank(wxOauthUser.getOpenId(), wxOauthUser.getUnionId())) {
            log.warn("RPCWxOauthUserServiceImpl addWxOauthUser 参数非法，req = {}", JSON.toJSONString(req));
            resp.setResult(CommonResultEnum.MISSING_REQUEST_PARAMETER);
            return resp;
        }
        MpAuthAccount mpAuthAccount = new MpAuthAccount();
        BeanUtils.copyProperties(wxOauthUser, mpAuthAccount);
        userInfoService.save(mpAuthAccount);

        return resp.setWxOauthUser(wxOauthUser.setId(mpAuthAccount.getId()));
    }

    @Override
    public QueryWxOauthUserResp queryWxOauthUser(QueryWxOauthUserReq req) {
        QueryWxOauthUserResp resp = new QueryWxOauthUserResp();
        String appId = req.getAppId();
        String openId = req.getOpenId();
        String unionId = req.getUnionId();
        Long id = req.getId();
        if (StringUtils.isEmpty(appId) || (StringUtils.isAllBlank(openId, unionId) && id == null)) {
            log.warn("RPCWxOauthUserServiceImpl queryWxOauthUser 参数非法，req = {}", JSON.toJSONString(req));
            resp.setResult(CommonResultEnum.MISSING_REQUEST_PARAMETER);
            return resp;
        }

        LambdaQueryWrapper<MpAuthAccount> queryWrapper = new LambdaQueryWrapper<MpAuthAccount>()
                .eq(id != null, MpAuthAccount::getId, id)
                .eq(StringUtils.isEmpty(appId), MpAuthAccount::getAppId, appId)
                .eq(StringUtils.isNotBlank(openId), MpAuthAccount::getOpenId, openId)
                .eq(StringUtils.isNotBlank(unionId), MpAuthAccount::getUnionId, unionId)
                .orderByDesc(MpAuthAccount::getId)
                .last(" LIMIT 1");

        MpAuthAccount mpAuthAccount = userInfoService.getOne(queryWrapper);
        if (mpAuthAccount == null) {
            log.warn("RPCWxOauthUserServiceImpl queryWxOauthUser 未查到数据，req = {}", JSON.toJSONString(req));
            return resp;
        }

        WxOauthUser wxOauthUser = new WxOauthUser();
        BeanUtils.copyProperties(mpAuthAccount, wxOauthUser);
        resp.setWxOauthUser(wxOauthUser);

        return resp;
    }

    @Override
    public UpdateWxOauthUserResp updateWxOauthUser(UpdateWxOauthUserReq req) {
        UpdateWxOauthUserResp resp = new UpdateWxOauthUserResp();
        WxOauthUser wxOauthUser = req.getWxOauthUser();
        if (wxOauthUser == null
                || StringUtils.isAllBlank(wxOauthUser.getOpenId(), wxOauthUser.getUnionId())
                || wxOauthUser.getId() == null
                || StringUtils.isEmpty(wxOauthUser.getAppId())) {
            log.warn("RPCWxOauthUserServiceImpl updateWxOauthUser 参数非法，req = {}", JSON.toJSONString(req));
            resp.setResult(CommonResultEnum.MISSING_REQUEST_PARAMETER);
            return resp;
        }
        MpAuthAccount mpAuthAccount = new MpAuthAccount();
        BeanUtils.copyProperties(wxOauthUser, mpAuthAccount);
        userInfoService.updateById(mpAuthAccount);
        return resp;

    }
}
